﻿using LinqKit;
using Management.Auxiliary;
using System.Linq;

namespace Management.Core.News
{
    public class NewsManager : BaseManager<News>
    {
        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="pagingNew">分页数据</param>
        /// <param name="newsId">产品ID</param>      
        /// <param name="order">排序【null（默认）-ID降序，0-ID升序，1-ID降序，2-注册时间降序，3-注册时间升序，4-最后登录时间升序，5-最后登录时间降序】</param>
        /// <returns></returns>
        public Paging<News> FindPageList(Paging<News> pagingNews, string keywords, int? contentType, int? order)
        {
            //查询表达式
            var _where = PredicateBuilder.New<News>(true);
            if (contentType != null && contentType > 0) _where = _where.And(u => u.ContentType == contentType);
            if (!string.IsNullOrEmpty(keywords)) _where = _where.And(u => u.Title.Contains(keywords) || u.Author.Contains(keywords) || u.Content.Contains(keywords));
            //排序
            OrderParam _orderParam;
            switch (order)
            {
                case 0://ID升序
                    _orderParam = new OrderParam() { PropertyName = "NewsID", Method = OrderMethod.ASC };
                    break;
                case 1://ID降序
                    _orderParam = new OrderParam() { PropertyName = "NewsID", Method = OrderMethod.DESC };
                    break;
                case 2://创建时间降序
                    _orderParam = new OrderParam() { PropertyName = "Created_at", Method = OrderMethod.ASC };
                    break;
                case 3://创建时间升序
                    _orderParam = new OrderParam() { PropertyName = "Created_at", Method = OrderMethod.DESC };
                    break;
                default://ID降序
                    _orderParam = new OrderParam() { PropertyName = "NewsID", Method = OrderMethod.DESC };
                    break;
            }

            pagingNews.Items = Repository.FindPageList(pagingNews.PageSize, pagingNews.PageIndex, out pagingNews.TotalNumber, _where, _orderParam).ToList();
            return pagingNews;
        }


    }
}
